using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class MMHubTileImgMgr
	{
		public static MMCache<ImageSource, MMHubTileImg> TileCache = new MMCache<ImageSource, MMHubTileImg>(100);
		public static MMHubTileImg GetHubTileImg(ImageSource originImg)
		{
			if (originImg == null)
			{
				return null;
			}
			if (MMHubTileImgMgr.TileCache.Exist(originImg))
			{
				return MMHubTileImgMgr.TileCache.GetDataFromCache(originImg);
			}
			MMHubTileImg bigAndSmailImg = MMHubTileImgMgr.GetBigAndSmailImg(originImg, 80, 80);
			MMHubTileImgMgr.TileCache.AddDataToCache(originImg, bigAndSmailImg);
			return bigAndSmailImg;
		}
		private static MMHubTileImg GetBigAndSmailImg(ImageSource originImg, int int_0, int int_1)
		{
			if (originImg == null)
			{
				return null;
			}
			WriteableBitmap writeableBitmap = null;
			if (originImg is BitmapImage)
			{
				Image image = new Image();
				image.set_Source(originImg);
				image.set_Stretch(1);
				image.set_Width((double)int_0);
				image.set_Height((double)int_1);
				writeableBitmap = new WriteableBitmap(int_0, int_1);
				writeableBitmap.Render(image, null);
				writeableBitmap.Invalidate();
			}
			else
			{
				if (originImg is WriteableBitmap)
				{
					writeableBitmap = (originImg as WriteableBitmap);
					if (writeableBitmap.get_PixelWidth() != int_0 || writeableBitmap.get_PixelHeight() != int_1)
					{
						Image image2 = new Image();
						image2.set_Source(originImg);
						image2.set_Stretch(1);
						image2.set_Width((double)int_0);
						image2.set_Height((double)int_1);
						writeableBitmap = new WriteableBitmap(int_0, int_1);
						writeableBitmap.Render(image2, null);
						writeableBitmap.Invalidate();
					}
				}
			}
			if (writeableBitmap == null)
			{
				return null;
			}
			return new MMHubTileImg
			{
				BigImg = MMHubTileImgMgr.GetBigImg(writeableBitmap, int_0, int_1),
				SmallImg = MMHubTileImgMgr.GetSmallImg(writeableBitmap, int_0, int_1)
			};
		}
		private static WriteableBitmap GetBigImg(ImageSource imageSource_0, int int_0, int int_1)
		{
			if (imageSource_0 == null)
			{
				return null;
			}
			if (imageSource_0 is BitmapImage)
			{
				Image image = new Image();
				image.set_Source(imageSource_0);
				image.set_Stretch(0);
				image.set_Width((double)int_0);
				image.set_Height((double)int_1);
				WriteableBitmap writeableBitmap = new WriteableBitmap(int_0, int_1);
				writeableBitmap.Render(image, null);
				writeableBitmap.Invalidate();
				return MMHubTileImgMgr.GetBigImg(writeableBitmap, int_0, int_1);
			}
			if (imageSource_0 is WriteableBitmap)
			{
				return MMHubTileImgMgr.GetBigImg(imageSource_0 as WriteableBitmap, int_0, int_1);
			}
			return null;
		}
		private static WriteableBitmap GetBigImg(WriteableBitmap writeableBitmap_0, int int_0, int int_1)
		{
			if (writeableBitmap_0 == null)
			{
				return null;
			}
			GeometryGroup geometryGroup = new GeometryGroup();
			RectangleGeometry rectangleGeometry = new RectangleGeometry();
			rectangleGeometry.set_Rect(new Rect(0.0, 0.0, (double)int_0, (double)(int_1 / 2)));
			geometryGroup.get_Children().Add(rectangleGeometry);
			RectangleGeometry rectangleGeometry2 = new RectangleGeometry();
			rectangleGeometry2.set_Rect(new Rect(0.0, (double)(int_1 / 2), (double)(int_0 / 2), (double)int_1));
			geometryGroup.get_Children().Add(rectangleGeometry2);
			Image image = new Image();
			image.set_Stretch(0);
			image.set_Source(writeableBitmap_0);
			Canvas canvas = new Canvas();
			canvas.set_Width((double)int_0);
			canvas.set_Height((double)int_1);
			canvas.get_Children().Add(image);
			canvas.set_Clip(geometryGroup);
			return new WriteableBitmap(canvas, null);
		}
		private static WriteableBitmap GetSmallImg(WriteableBitmap writeableBitmap_0, int int_0, int int_1)
		{
			if (writeableBitmap_0 == null)
			{
				return null;
			}
			return MMHubTileImgMgr.CutImage(writeableBitmap_0, (double)(int_0 / 2), (double)(int_1 / 2), (double)(int_0 / 2), (double)(int_1 / 2));
		}
		private static WriteableBitmap CutImage(WriteableBitmap writeableBitmap_0, double double_0, double double_1, double width, double height)
		{
			Image image = new Image();
			image.set_Stretch(0);
			UIElement arg_37_0 = image;
			TranslateTransform translateTransform = new TranslateTransform();
			translateTransform.set_X(-1.0 * double_0);
			translateTransform.set_Y(-1.0 * double_1);
			arg_37_0.set_RenderTransform(translateTransform);
			image.set_Source(writeableBitmap_0);
			Canvas canvas = new Canvas();
			canvas.set_Width(width);
			canvas.set_Height(height);
			canvas.get_Children().Add(image);
			return new WriteableBitmap(canvas, null);
		}
	}
}
